package br.com.ecommerce.view.form;

import br.com.ecommerce.entity.IEntity;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rpateixeira
 */
public abstract class FormAbstrato<T extends IEntity> {
    public final String INSERT="INSERT";
    public final String UPDATE="UPDATE";
    public final String REMOVE="REMOVE";
    public final String SEARCH="SEARCH";
    private T entity;

    public abstract String insert();
    public abstract String update();
    public abstract String remove();
    public abstract String search();

    public T getEntity() {
        if(entity==null){     
            entity = createEntity();
        }
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public T createEntity(){
        try {
            return ((Class<T>) ((ParameterizedType) ((Type)getClass().getGenericSuperclass())).getActualTypeArguments()[0]).newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(FormAbstrato.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FormAbstrato.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String limparEntity(){
        setEntity(createEntity());
        return null;
    }
}
